/**
 * 静态方法，接收一个可迭代的promise对象，返回一个promise，当所有的promise的状态敲定时，返回的promise变为fulfilled，并且带有所有promise的结果数组
 * 结果数组里的每一个item应该是如下结构:{ status: 'fulfilled'/'rejected', value/reason }
 * @param {*} promiseList 
 * @returns 
 */
function promiseAllSettled(promiseList) {
    
    return new Promise((resolve, reject) => {
        if(!promiseList.length) {
            resolve([]);
            return;
        }

        let count = 0;
        const result = Array(promiseList.length);
        promiseList.forEach((promise, index) => {
            Promise.resolve(promise)
            .then((res) => {
                result[index] = { status: 'fulfilled', value: res };
            })
            .catch((error) => {
                result[index] = { status: 'rejected', reason: error };
            })
            .finally(() => {
                count++;
                if (count === promiseList.length) {
                    resolve(result);
                }
            });
        });
    });
}